window.onload = function() {
    let username = document.getElementById("username")
    let password = document.getElementById("password")
    console.log(username.value)
    username.addEventListener("input", (e) => {
        console.log("输入了", e.target.value)
    })
    let s1 = document.getElementById("s1")
    let s2 = document.getElementById("s2")
    s1.addEventListener("change", (e) => {
        console.log("男", e.target.checked)
    })
    s2.addEventListener("change", (e) => {
        console.log("女", e.target.checked)
    })
    let a = document.querySelector("#a")
    console.log(a.value)
    a.addEventListener("change", (e) => {
        console.log(e.target.value)
    })
    let stop = document.getElementsByTagName("button")[0]
    stop.addEventListener("click", (e) => {
        e.preventDefault();
        let name = username.value
        let psd = password.value
        let xhr = new XMLHttpRequest()
            //open连接通道
        xhr.open("GET", "http://127.0.0.1:8080/login" + "?username=" + name + "&password=" + psd, false)
            //异步请求状态readystate
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                console.log("请求成功，表单提交成功")
            }
        }

        xhr.send()
        if (xhr.readyState === 4 && xhr.status === 200) {
            console.log("请求成功，表单提交成功")
        }

        //post(fetch)
        document.getElementById('postForm').addEventListener('submit', async(e) => {
            e.preventDefault(); // ① 阻止默认跳转
            const payload = Object.fromEntries(new FormData(e.target)); // ② 转对象
            try {
                const res = await fetch('http://127.0.0.1:8080/login', { // ③ 发请求
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(payload)
                });
                const text = await res.text();
                console.log('POST 成功', text);
            } catch (err) {
                console.error('POST 失败', err);
            }
        })
    })

}